/*
 * @Author: Tendo 
 * @Date: 2021-10-15 14:20:54 
 * @Last Modified by:   Tendo 
 * @Last Modified time: 2021-10-15 14:20:54 
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var largestValues = function (root) {
    let res = [], queue = [];
    queue.push(root);
    while (queue.length && root !== null) {
        // 记录当前节点数量
        let length = queue.length;
        let maxValue = queue[0];
        while (length--) {
            let node = queue.shift();
            maxValue = maxValue > node.val ? maxValue : node.val;
            node.left && queue.push(node.left);
            node.right && queue.push(node.right);
        }
        res.push(maxValue);
    }
    return res;
};